package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.comment.util.RedisUtil;
import com.woniu.controller.form.AddJunkTypeForm;
import com.woniu.controller.form.ChangeFatherForm;
import com.woniu.controller.form.ChangeStatusFrom;
import com.woniu.controller.form.PageForm;
import com.woniu.model.JunkType;
import com.woniu.model.Result;
import com.woniu.service.JunkTypeService;
import com.woniu.service.param.AddJunkTypeParam;
import com.woniu.service.param.ChangeFatherParam;
import com.woniu.service.param.ChangeStatusParam;
import com.woniu.service.param.PageParam;
import com.woniu.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  垃圾种类相关接口
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@Api(tags = "垃圾种类相关接口")
@RequestMapping("/junkType")
public class JunkTypeController {
    @Resource
    private JunkTypeService junkTypeService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 根据id查找垃圾种类
     * id 垃圾种类id
     */
    @GetMapping("/getById/{id}")
    @ApiOperation("根据id查找垃圾种类")
    public Result<JunkType> getById(@PathVariable("id") Integer id) throws Exception {
        JunkType JunkType = junkTypeService.getById(id);
        return Result.newInstance(JunkType.class).setCode(200).setData(JunkType).setMessage("根据id查找垃圾种类成功!");
    }

    /**
     * 根据id集合查找垃圾种类
     * list 垃圾种类id集合
     */
    @PostMapping("/getByIdList")
    @ApiOperation("根据id集合查找垃圾种类")
    public Result<List<JunkType>> getByIdList(@RequestBody List<Integer> ids) throws Exception {
        List<JunkType> JunkType = junkTypeService.getByIdList(ids);
        return Result.newListInstance(JunkType.class).setCode(200).setData(JunkType).setMessage("根据id集合查找垃圾种类成功!");
    }

    /**
     * 添加垃圾种类
     */
    @PostMapping("/addJunkType")
    @ApiOperation("添加垃圾种类")
    public Result<?> addJunkType(@RequestBody AddJunkTypeForm form,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        JwtUtil.parseAdminToken(refreshToken);
        junkTypeService.addJunkType(BeanUtil.toBean(form, AddJunkTypeParam.class));
        return Result.newInstance(null).setCode(200).setMessage("添加垃圾种类成功!");
    }

    /**
     * 修改垃圾种类
     */
    @PostMapping("/changeJunkType")
    @ApiOperation("修改垃圾种类的分类")
    public Result<?> changeJunkType(@RequestBody ChangeFatherForm form,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        JwtUtil.parseAdminToken(refreshToken);
        junkTypeService.changeJunkType(BeanUtil.toBean(form, ChangeFatherParam.class));
        return Result.newInstance(null).setCode(200).setMessage("修改垃圾种类的分类成功!");
    }

    /**
     * 分页查询垃圾种类
     * pageNum,pageSize
     */
    @PostMapping("/loadAllJunkType")
    @ApiOperation("分页查询垃圾种类")
    public Result<?> loadAllJunkType(@RequestBody PageForm form){
        Page<JunkType> bookTypes =  junkTypeService.loadAllJunkType(BeanUtil.toBean(form, PageParam.class));
        return Result.newPageInstance(JunkType.class).setData(bookTypes).setCode(200).setMessage("分页查询所有垃圾种类成功!");
    }

    /**
     * 更改垃圾类型状态
     * typeState 预期修改状态
     * typeId 垃圾类型id
     */
    @PostMapping("/changeStatus")
    @ApiOperation("修改垃圾种类状态")
    public Result<?> changeStatus(@RequestBody ChangeStatusFrom form,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        JwtUtil.parseAdminToken(refreshToken);
        junkTypeService.changeStatus(BeanUtil.toBean(form, ChangeStatusParam.class));
        return Result.newInstance(null).setCode(200).setMessage("修改垃圾种类状态成功!");
    }

    /**
     * 根据id查找是否存在(仅父级)
     * id 垃圾种类id
     */
    @GetMapping("/getByIdFather/{id}")
    @ApiOperation("根据id查找是否存在(仅父级)")
    public Result<JunkType> getByIdFather(@PathVariable("id") Integer id) throws Exception {
        JunkType JunkType = junkTypeService.getByIdFather(id);
        return Result.newInstance(JunkType.class).setCode(200).setData(JunkType).setMessage("根据id查找是否存在(仅父级)成功!");
    }

    /**
     * 用户查看垃圾种类
     * list 所有垃圾种类集合
     */
    @GetMapping("/checkJunkType")
    @ApiOperation("用户查看所有垃圾种类")
    Result<List<JunkType>> checkJunkType(){
        List<JunkType> JunkType = junkTypeService.checkJunkType();
        return Result.newListInstance(JunkType.class).setCode(200).setData(JunkType).setMessage("用户查看所有垃圾种类成功!");
    }

    /**
     * 根据垃圾id查询垃圾单位
     * String 单位
     */
    @GetMapping("/getUnitById/{id}")
    @ApiOperation("根据垃圾id查询垃圾单位")
    Result<String> getUnitById(@PathVariable("id") Integer id){
        String unit = junkTypeService.getUnitById(id);
        return Result.newInstance(String.class).setCode(200).setData(unit).setMessage("根据垃圾id查询垃圾单位成功!");
    }
}

